//
//  HitArea.m
//  FingerChinese
//
//  Created by luar on 30/01/2010.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "HitArea.h"

@implementation HitArea
@synthesize mp3Path, pageNo, canPlay;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
	canPlay = YES;
	//NSLog(@"HitArea %f %f %f %f", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
	self.backgroundColor = [UIColor clearColor];
	[self addTarget:self action:@selector(playSound) 
		forControlEvents:UIControlEventTouchDown];
	/*[self addTarget:self action:@selector(dehighlight) 
		forControlEvents:UIControlEventTouchUpInside];
	[self addTarget:self action:@selector(dehighlight) 
		forControlEvents:UIControlEventTouchUpOutside];*/
	return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	NSSet *allTouches = [event allTouches];
    int count = [allTouches count];
	if (count>1) {
		canPlay = NO;
	}
	[super touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event];
	NSDictionary *dict = [NSDictionary dictionaryWithObject:event forKey:@"event"];
	[[NSNotificationCenter defaultCenter] postNotificationName:@"PageTouchesBegan" object:self userInfo:dict];
}
- (void)touchesMove:(NSSet *)touches withEvent:(UIEvent *)event {
	[self stopSound];
	//[super touchesMove:(NSSet *)touches withEvent:(UIEvent *)event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	[super touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event];
	NSSet *allTouches = [event allTouches];
	UITouch *touch = [[event allTouches] anyObject];
	//NSLog(@"touchesEnded %d", touch.tapCount);
	if (touch.tapCount > 1) {
		[self stopSound];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"DoubleTapZoom" object:self];
	} 
	if ([allTouches count] > 1) {
		canPlay = YES;
	}
}

- (void)playSound {
	if (canPlay) {
		if (pageNo == 0) {
			canPlay = NO;
			//NSLog(@"hitArea touch");
			NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:mp3Path, [[NSBundle mainBundle] resourcePath]]];
			
			NSError *error;
			if (audioPlayer) {
				[audioPlayer release];
			}
			audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
			audioPlayer.delegate = self;
			//audioPlayer.numberOfLoops = 0;
			
			if (audioPlayer == nil) {
				NSLog([error description]);				
			} else {
				[audioPlayer play];
				[self highlight];
			}			
		} else {
			[self highlight];
			[[NSNotificationCenter defaultCenter] postNotificationName:@"JumpPage" 
																object:self];
		}
	}
}

- (void)stopSound {
	NSLog(@"stopSound");
	[audioPlayer stop];
	canPlay = YES;
	[self dehighlight];
}

- (void)highlight {
	self.backgroundColor = [UIColor blackColor]; 
	self.alpha = 0.3;
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.3];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(onFadeInDone:finished:context:)];
	self.alpha = 0.0;
	[UIView commitAnimations];
}

- (void)onFadeInDone:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
	[self dehighlight];
}

- (void)dehighlight {
	self.backgroundColor = [UIColor clearColor]; 
	self.alpha = 1; 
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { 
	//NSLog(@"audioPlayerDidFinishPlaying");
	canPlay = YES;
	//[audioPlayer dealloc];
}

- (void)dealloc {
	//NSLog(@"HitArea dealloc");
	//[audioPlayer release];
	[audioPlayer dealloc];
    [super dealloc];
}

@end
